package com.gzx.qc.modular.check.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gzx.qc.modular.asr.entity.GzxAsrTask;
import com.gzx.qc.modular.asr.service.GzxAsrDbService;
import com.gzx.qc.modular.check.bo.GzxCallCheckEditBo;
import com.gzx.qc.modular.check.bo.GzxCallCheckQueryBo;
import com.gzx.qc.modular.check.entity.GzxCallCheck;
import com.gzx.qc.modular.check.mapper.GzxCallCheckMapper;
import com.gzx.qc.modular.check.service.GzxCallCheckDbService;
import com.gzx.qc.modular.check.service.GzxCallCheckService;
import com.gzx.qc.modular.check.vo.GzxCallCheckVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.mvc.service.impl.BaseServiceImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zjt
 * @description
 * @date 2025/7/29 15:30
 */
@DS("qc")
@Service
public class GzxCallCheckServiceImpl extends BaseServiceImpl<GzxCallCheckEditBo, GzxCallCheckVo, GzxCallCheckQueryBo, GzxCallCheckMapper, GzxCallCheck> implements GzxCallCheckService {

    private final GzxCallCheckDbService callCheckDbService;

    private final GzxAsrDbService asrDbService;

    public GzxCallCheckServiceImpl(GzxCallCheckMapper baseMapper, GzxCallCheckDbService callCheckDbService, GzxAsrDbService asrDbService) {
        super(baseMapper);
        this.callCheckDbService = callCheckDbService;
        this.asrDbService = asrDbService;
    }

    @Override
    protected LambdaQueryWrapper<GzxCallCheck> buildQueryWrapper(GzxCallCheckQueryBo queryBo) {
        return Wrappers.<GzxCallCheck>lambdaQuery()
                .select(GzxCallCheck::getId, GzxCallCheck::getCallType, GzxCallCheck::getCallId, GzxCallCheck::getCallee, GzxCallCheck::getCallDuration, GzxCallCheck::getKeyword, GzxCallCheck::getAgentNum, GzxCallCheck::getUserName, GzxCallCheck::getDeptId, GzxCallCheck::getAiReviewStatus, GzxCallCheck::getAiReviewResult, GzxCallCheck::getBranchReviewStatus, GzxCallCheck::getHqReviewStatus, GzxCallCheck::getFinalReviewStatus, GzxCallCheck::getFileUrl)
                .like(StringUtils.isNotBlank(queryBo.getKeyword()), GzxCallCheck::getKeyword, queryBo.getKeyword())
                .eq(StringUtils.isNotBlank(queryBo.getCallId()), GzxCallCheck::getCallId, queryBo.getCallId())
                .eq(StringUtils.isNotBlank(queryBo.getCallee()), GzxCallCheck::getCallee, queryBo.getCallee())
                .ge(queryBo.getCallDuration() != null, GzxCallCheck::getCallDuration, queryBo.getCallDuration())
                .eq(StringUtils.isNotBlank(queryBo.getAgentNum()), GzxCallCheck::getAgentNum, queryBo.getAgentNum())
                .eq(queryBo.getDeptId() != null, GzxCallCheck::getDeptId, queryBo.getDeptId())
                .in(CollectionUtil.isNotEmpty(queryBo.getAiReviewStatus()), GzxCallCheck::getAiReviewStatus, queryBo.getAiReviewStatus())
                .in(CollectionUtil.isNotEmpty(queryBo.getBranchReviewStatus()), GzxCallCheck::getBranchReviewStatus, queryBo.getBranchReviewStatus())
                .in(CollectionUtil.isNotEmpty(queryBo.getHqReviewStatus()), GzxCallCheck::getHqReviewStatus, queryBo.getHqReviewStatus())
                .in(CollectionUtil.isNotEmpty(queryBo.getFinalReviewStatus()), GzxCallCheck::getFinalReviewStatus, queryBo.getFinalReviewStatus())
                .between(StringUtils.isNotBlank(queryBo.getBeginTime()) && StringUtils.isNotBlank(queryBo.getEndTime()), GzxCallCheck::getCreateTime, queryBo.getBeginTime(), queryBo.getEndTime())
                .orderByDesc(GzxCallCheck::getId);
    }

    /**
     * 保存质检相关的话单
     *
     * @param consumerRecordList
     */
    @Override
    public void saveCallCheck(List<ConsumerRecord<String, String>> consumerRecordList) {
        List<GzxCallCheck> callCheckList = new ArrayList<>();
        List<GzxAsrTask> asrTaskList = new ArrayList<>();
        for (ConsumerRecord<String, String> consumerRecord : consumerRecordList) {
            GzxCallCheck callCheck = JSONObject.parseObject(consumerRecord.value(), GzxCallCheck.class);
            callCheckList.add(callCheck);
            GzxAsrTask asrTask = new GzxAsrTask();
            asrTask.setCallId(callCheck.getCallId());
            asrTask.setFileUrl(callCheck.getFileUrl());
            asrTaskList.add(asrTask);
        }
        Thread.startVirtualThread(() -> callCheckDbService.insertBatch(callCheckList));
        Thread.startVirtualThread(() -> asrDbService.batchSaveTask(asrTaskList));
    }

    /**
     * 质检审批
     *
     * @param callCheckEditBo
     */
    @Override
    public void review(GzxCallCheckEditBo callCheckEditBo) {
        GzxCallCheck callCheck = callCheckDbService.selectOne(Wrappers.<GzxCallCheck>lambdaQuery()
                .select(GzxCallCheck::getId, GzxCallCheck::getAiReviewStatus, GzxCallCheck::getBranchReviewStatus, GzxCallCheck::getHqReviewStatus, GzxCallCheck::getFinalReviewStatus)
                .eq(GzxCallCheck::getId, callCheckEditBo.getId()));
        if (callCheck != null && callCheck.getAiReviewStatus() > 0) {
            String node = callCheckEditBo.getNode();
            Integer status = callCheckEditBo.getStatus();
            String result = callCheckEditBo.getResult();
            if ("BRANCH".equals(node) && callCheck.getBranchReviewStatus() == 0 && callCheck.getHqReviewStatus() == 0 && callCheck.getFinalReviewStatus() == 0) {
                callCheck.setBranchReviewStatus(status);
                callCheck.setBranchReviewResult(result);
            } else if ("HQ".equals(node) && callCheck.getBranchReviewStatus() > 0 && callCheck.getHqReviewStatus() == 0 && callCheck.getFinalReviewStatus() == 0) {
                callCheck.setHqReviewStatus(status);
                callCheck.setHqReviewResult(result);
            } else if ("FINAL".equals(node) && callCheck.getBranchReviewStatus() > 0 && callCheck.getHqReviewStatus() > 0 && callCheck.getFinalReviewStatus() == 0) {
                callCheck.setFinalReviewStatus(status);
                callCheck.setFinalReviewResult(result);
            } else {
                throw new CommonException("当前节点审批流程有误");
            }
            callCheckDbService.updateById(callCheck);
        }
    }
}
